package com.example.fw;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import com.example.tests.ContactObject;

public class ContactHelper extends HelperWithWebDriverBase{

	private HashSet<ContactObject> contactCache;

	public ContactHelper(ApplicationManager manager){
		super(manager);
	}
	
	public Set<ContactObject> getContacts() {
		if (contactCache != null) {
			buildContactCache();
		}
		return contactCache;
	}
	private void buildContactCache(){
		contactCache = new HashSet<ContactObject>();
		manager.getNavigationHelper().openMainPage();
		WebElement form = findElements(By.tagName("form")).get(1);
		List<WebElement> checkboxes = form.findElements(By.name("selected[]"));
		Set<ContactObject> contacts = new HashSet<ContactObject>();
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			Pattern p = Pattern.compile(".*\\((.*)\\)");
			Matcher m = p.matcher(title);
			if (m.matches()) {
				title = m.group(1);
				System.out.println("Contacts:"+m.group(1));
			}
			
			//title = title.substring("Select (".length(), title.length()-")".length());
			ContactObject contact = new ContactObject()
				.setFirstName(title)
				.setId(checkbox.getAttribute("value"));
			contactCache.add(contact);
		}
	}
	
	public void createContact(ContactObject contactObject) {
		manager.getNavigationHelper().openMainPage();
		addNewContact();
		fillContactForm(contactObject);
		submitForm ();
		manager.getNavigationHelper().returnToHomePage();
		contactCache = null;
	}

	public void deleteContact(int i) {
		manager.getNavigationHelper().openMainPage();
		click(By.cssSelector("img[alt=\"Edit\"]"));
		click(By.xpath("(//input[@name='update'])[2]"));
		manager.getNavigationHelper().returnToHomePage();
		contactCache = null;
	}
	
	public void modifyContact(int i, ContactObject contactObject) {
		manager.getNavigationHelper().openMainPage();
		modifyContact();
		fillContactForm(contactObject);
		submitModifiedForm ();
		manager.getNavigationHelper().returnToHomePage();
		contactCache = null;
	}
	
	private void submitModifiedForm() {
		click(By.xpath("(//input[@name='update'])[2]"));
	}


	private void modifyContact() {
		click(By.cssSelector("img[alt=\"Edit\"]"));
		
	}


	protected void submitForm() {
		click(By.name("submit"));
	}


	private void fillContactForm(ContactObject contactObject) {
		type ("firstname", contactObject.firstname);
		type ("lastname", contactObject.lastname);
		type ("address", contactObject.address);
		type ("home", contactObject.home);
		type ("mobile", contactObject.mobile);
		type ("work", contactObject.work);
		type ("email", contactObject.email);
		type ("email2", contactObject.email2);
		type ("byear", contactObject.byear);
		type ("address2", contactObject.address2);
		type ("phone2", contactObject.phone2);
		new Select(findElement(By.name("bday"))).selectByVisibleText("1");
		new Select(findElement(By.name("bmonth"))).selectByVisibleText("January");		
	}

	private void addNewContact() {
		click(By.linkText("add new"));
	}






}
